package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.delegate.PayProofDetailDelegate;
import com.xmb.wechat.bean.PayProofBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/20 18:04
 */
public class WechatFundPayProofDetailActivity extends BaseActivity {

    @BindView(R2.id.iv_bg)
    ImageView mIvBg;
    private PayProofDetailDelegate mDelegate;

    public WechatFundPayProofDetailActivity() {
        super(R.layout.activity_fund_pay_proof_detail);
    }

    public static void start(Activity activity, String bg, ArrayList<PayProofBean> data) {
        Intent intent = new Intent(activity, WechatFundPayProofDetailActivity.class);
        intent.putExtra("bg", bg);
        intent.putExtra("data", data);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        setStatusBarColor(Color.parseColor("#ededed"));
        setNavigationColor(Color.parseColor("#f7f7f7"));

        mDelegate = new PayProofDetailDelegate(this);
        //数据
        ArrayList<PayProofBean> arrayList = getIntent().getParcelableArrayListExtra("data");
        if (arrayList != null && arrayList.size() > 0)
            sortData(arrayList);
        //背景
        String bg = getIntent().getStringExtra("bg");
        if (bg != null)
            Picasso.get().load("file://" + bg).into(mIvBg);
    }

    private void sortData(ArrayList<PayProofBean> data) {
        ArrayList<PayProofBean> result = new ArrayList<>();
        PayProofBean pre = null;
        for (PayProofBean payProofBean : data) {
            if (pre != null) {//不是第一条数据
                if (!isDateSame(pre, payProofBean)){
                    PayProofBean time = new PayProofBean();
                    time.setMsgTime(payProofBean.getMsgTime());
                    result.add(time);
                }
            } else {//第一条数据
                PayProofBean time = new PayProofBean();
                time.setMsgTime(payProofBean.getMsgTime());
                result.add(time);
            }
            result.add(payProofBean);
            pre = payProofBean;
        }
        mDelegate.addData(result);
    }

    //比较两个时间是否一样(时 分)
    @SuppressLint("SimpleDateFormat")
    private boolean isDateSame(PayProofBean pre, PayProofBean payProofBean) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return TextUtils.equals(simpleDateFormat.format(new Date(pre.getMsgTime())), simpleDateFormat.format(new Date(payProofBean.getMsgTime())));
    }

    @OnClick(R2.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
